<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\GzdKaoQinExcelExpoter;
use App\Models\GzdKaoqinbiaoMubanDetail;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class GzdKaoqinbiaoMubanDetailController extends AdminController
{
    protected $title = '考勤表详情';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GzdKaoqinbiaoMubanDetail(), function (Grid $grid) {

            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableViewButton();
            $grid->disableDeleteButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('muban_id', '模板')->select(function ($muban_id){
                    return DB::table('gzd_muban')->where('deleted_at',null)->pluck('name as text','id');
                })->width(5);

            });

            if(Admin::user()->sensitive == 0){
                $grid->export(new GzdKaoQinExcelExpoter());
            }
            $grid->model()->orderBy('id', 'asc');

            $grid->number('序号');

            $grid->column('name');
            $grid->column('time_slot');

            $grid->column('k1','1')->editable(true);
            $grid->column('k2','2')->editable(true);
            $grid->column('k3','3')->editable(true);
            $grid->column('k4','4')->editable(true);
            $grid->column('k5','5')->editable(true);
            $grid->column('k6','6')->editable(true);
            $grid->column('k7','7')->editable(true);
            $grid->column('k8','8')->editable(true);
            $grid->column('k9','9')->editable(true);
            $grid->column('k10','10')->editable(true);
            $grid->column('k11','11')->editable(true);
            $grid->column('k12','12')->editable(true);
            $grid->column('k13','13')->editable(true);
            $grid->column('k14','14')->editable(true);
            $grid->column('k15','15')->editable(true);
            $grid->column('k16','16')->editable(true);
            $grid->column('k17','17')->editable(true);
            $grid->column('k18','18')->editable(true);
            $grid->column('k19','19')->editable(true);
            $grid->column('k20','20')->editable(true);
            $grid->column('k21','21')->editable(true);
            $grid->column('k22','22')->editable(true);
            $grid->column('k23','23')->editable(true);
            $grid->column('k24','24')->editable(true);
            $grid->column('k25','25')->editable(true);
            $grid->column('k26','26')->editable(true);
            $grid->column('k27','27')->editable(true);
            $grid->column('k28','28')->editable(true);
            $grid->column('k29','29')->editable(true);
            $grid->column('k30','30')->editable(true);
            $grid->column('k31','31')->editable(true);

            $grid->column('sum');
//            $grid->column('remarks');
//            $grid->column('muban_id');
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->setActionClass(Grid\Displayers\ContextMenuActions ::class);


        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GzdKaoqinbiaoMubanDetail(), function (Show $show) {
            $show->field('id');
            $show->field('1');

            $show->field('name');
            $show->field('time_slot');
            $show->field('sum');
            $show->field('remarks');
            $show->field('muban_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GzdKaoqinbiaoMubanDetail(), function (Form $form) {
            $form->display('id');
            $form->text('k1');
            $form->text('k2');
            $form->text('k3');
            $form->text('k4');
            $form->text('k5');
            $form->text('k6');
            $form->text('k7');
            $form->text('k8');
            $form->text('k9');
            $form->text('k10');
            $form->text('k11');
            $form->text('k12');
            $form->text('k13');
            $form->text('k14');
            $form->text('k15');
            $form->text('k16');
            $form->text('k17');
            $form->text('k18');
            $form->text('k19');
            $form->text('k20');
            $form->text('k21');
            $form->text('k22');
            $form->text('k23');
            $form->text('k24');
            $form->text('k25');
            $form->text('k26');
            $form->text('k27');
            $form->text('k28');
            $form->text('k29');
            $form->text('k30');
            $form->text('k31');
            $form->text('name');
            $form->text('time_slot');
            $form->text('sum');
            $form->text('remarks');
            $form->text('muban_id');

            $form->display('created_at');
            $form->display('updated_at');


            $form->saved(function (Form $form, $result) {

                $data = $form->repository()->model()->toArray();

                $sum = 0;
                for ($a=1;$a<=31;$a++){
                    $lie = 'k'.$a;
                    $sum += $data[$lie];
                }

                DB::table('gzd_kaoqinbiao_muban_details')->where('id',$data['id'])->update(['sum'=>$sum]);

                $new_sum = DB::table('gzd_kaoqinbiao_muban_details')->where('gzd_muban_details_id',$data['gzd_muban_details_id'])->sum('sum');

                $gzd_muban_details = DB::table('gzd_muban_details')->where('id',$data['gzd_muban_details_id'])->first();

                $quantity = $new_sum / 10;
                $payable = $gzd_muban_details->basic_salary * $quantity;
                $advance = $payable - $gzd_muban_details->actual - $gzd_muban_details->other;
                DB::table('gzd_muban_details')->where('id',$data['gzd_muban_details_id'])->update(['quantity'=>$quantity,'payable'=>$payable,'advance'=>$advance]);

            });


        });
    }
}
